﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Opc.Ua;
using Opc.Ua.Client;
using SiemensOPC;
using VswService_V7.Config;
using VswService_V7.DataBase.Tables;
using VswService_V7.Utility;

namespace VswService_V7.OPC.DB
{
    public class StripUnloadAddressDbData : SubscriptionDbDataBase
    {
        public StripUnloadAddressDbData(SimaticOPCClient opc, NodeConfig nodes) : base(opc, nodes)
        {
        }
        string askStripNo = "0";
        public override void CreateSubscription()
        {
            try
            {
                opcServer.QuickAddMonitorItem(opcNodes.StripUnLoadAddress.GrpoupName,
                    opcNodes.StripUnLoadAddress.PublishTime,
                    opcNodes.StripUnLoadAddress.SampleTime,
                    opcNodes.StripUnLoadAddress.GetNodeAddress(),
                    opcNodes.StripUnLoadAddress.GetNodeDisplayName(),
                    SubscriptionHandle_StripUnLoadAddress);

                opcServer.QuickAddMonitorItem(opcNodes.StripUnLoadAskAddress.GrpoupName,
                    opcNodes.StripUnLoadAskAddress.PublishTime,
                    opcNodes.StripUnLoadAskAddress.SampleTime,
                    opcNodes.StripUnLoadAskAddress.GetNodeAddress(),
                    opcNodes.StripUnLoadAskAddress.GetNodeDisplayName(),
                    SubscriptionHandleAsk_StripUnLoadAskAddress);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("StripUnloadAddressDbData CreateSubscription", ex);
                throw ex;
            }

        }

        private void SubscriptionHandle_StripUnLoadAddress(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null || notification.Value.Value == null)
                return;
            if (monitoredItem.DisplayName == "StripUnLoadAsk")
            {
                bool ask = false;
                if (bool.TryParse(notification.Value.Value.ToString(), out ask))
                {
                    if (ask)
                    {
                        Console.WriteLine("get unload strip ask:");
                        Task.Run(() => HandleUnloadStrip());
                    }
                    else
                    {
                        Console.WriteLine("replay unload strip ask:");
                        opcServer.WriteValue("0", opcNodes.StripUnLoadAskAddress.Result);
                        opcServer.WriteValue("false", opcNodes.StripUnLoadAddress.StripUnLoadRsp);

                    }
                }
            }
        }

        private void SubscriptionHandleAsk_StripUnLoadAskAddress(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null || notification.Value.Value == null)
                return;

            if (monitoredItem.DisplayName == "Strip_No")
            {
                try
                {
                    askStripNo = notification.Value.Value.ToString();
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLog("SubscriptionHandleAsk_StripUnLoadAskAddress  Strip_No is null", ex);
                    askStripNo = "0";
                }
            }
        }


        private async Task HandleUnloadStrip()
        {
            await Task.Delay(500);
            if (!string.IsNullOrEmpty(askStripNo) && askStripNo != "0")
            {
                try
                {
                    bool isExist = STRIP_INFO.UnloadStripNo(askStripNo);
                    if (isExist)
                    {
                        //卸料后判断Task状态并改变
                        TASK_INFO.UpdateTaskStatus(askStripNo);
                        opcServer.WriteValue("1", opcNodes.StripUnLoadAskAddress.Result);
                        Console.WriteLine("stripNo:  " + askStripNo + "  delete OK");
                    }
                    else
                    {
                        opcServer.WriteValue("3", opcNodes.StripUnLoadAskAddress.Result);
                        Console.WriteLine("stripNo:  " + askStripNo + "  delete failed");
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLog("HandleUnloadStrip", ex);
                    opcServer.WriteValue("2", opcNodes.StripUnLoadAskAddress.Result);
                }
            }
            else
            {
                opcServer.WriteValue("3", opcNodes.StripUnLoadAskAddress.Result);
                LogHelper.WriteLog("HandleUnloadStrip", new Exception("SRIPENO INVAILED delete FAILED"));

            }
            opcServer.WriteValue("true", opcNodes.StripUnLoadAddress.StripUnLoadRsp);
        }



    }
}
